#pragma once
#include<assert.h>

namespace bit
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != end)
			{
				push_back(*first);
				first++;
			}
		}

		vector(size_t n, const T& val = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		vector(int n, const T& val = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (auto& e : v)
			{
				push_back(e);
			}
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstorage, v._endofstorage);
		}

		vector<T>& operator=(vector<T> tmp)
		{
			swap(tmp);
			return *this;
		}

		vector()
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{}

		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstorage = nullptr;
		}



		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t sz = _finish - _start;
				T* tmp = new T[n];
				size_t i = 0;
				for (auto s : *this)
				{
					tmp[i] = s;
					i++;
				}
				delete[]_start;
				_start = tmp;
				_finish = tmp + sz;
				_endofstorage = tmp + n;

			}
		}

		void resize(size_t n, const T& val = T())
		{
			if (n < size())
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n);
				while (_finish != _endofstorage)
				{
					*_finish = val;
					_finish++;
				}
			}
		}

		void push_back(const T& x)
		{

			/*if (_finish == _endofstorage)
			{*/
				//size_t sz = _finish - _start;
				/*T* tmp = new T[_endofstorage - _start + 1];
				memset(tmp, _start, sizeof(T) * (_endofstorage - _start));
				delete[] _start;
				_start = tmp;
				_finish = _start + sz;
				_start[_finish] = x;
				_finish++;*/


				/*size_t sz = size();
				size_t cp = capacity() == 0 ? 4 : capacity() * 2;
				T* tmp = new T[cp];
				if (_start)
				{
					memcpy(tmp, _start, sizeof(T) * sz);
					delete[]_start;
					
				}
				_start = tmp;
				_finish = _start + sz;
				_endofstorage = _start + cp;
			}
			*_finish = x;
			_finish++;*/
			insert(end(), x);

		}

		void insert(iterator pos, const T& x)
		{
			assert(pos <= _finish);
			assert(pos >= _start);
			if (_finish == _endofstorage)
			{
				size_t sz = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + sz;
			}
			iterator end = _finish;
			while (end > pos)
			{
				*end = *(end - 1);
				end--;
			}
			*pos = x;
			++_finish;
		}

		iterator erase(iterator pos)
		{
			assert(pos < _finish);
			assert(pos >= _start);
			iterator start = pos+1;
			while (start < _finish)
			{
				*(start-1) = *start;
				start++;
			}
			_finish--;
			return pos;
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}

		const T& operator[](size_t pos) const
		{
			assert(pos < size());
			return _start[pos];
		}

		size_t capacity()
		{
			return _endofstorage - _start;
		}

		size_t size()
		{
			return _finish - _start;
		}



		//vector(size_t n, const T& x = 0)
		//{

		//}


	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;

	};

	void test2()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(1);
		v.push_back(1);
		v.push_back(1);
		v.push_back(1);
		cout << v.size() << "  " << v.capacity() << endl;
		v.reserve(100);
		cout << v.size() << "  " << v.capacity() << endl;
		v.insert(v.begin(), 10);
		for (auto s : v)
		{
			cout << s << "  ";
		}
		cout << endl;
		v.insert(v.end(), 66);
		for (auto s : v)
		{
			cout << s << "  ";
		}
		cout << endl;
		v.insert(v.begin()+3, 100);
		for (auto s : v)
		{
			cout << s << "  ";
		}
		cout << endl;
	}

	void test3()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		vector<int>::iterator it = v.begin();
		while (it != v.end())
		{
			if (*it % 2 == 0)
			{
				v.erase(it);
			}
			else
			{
				it++;
			}
		}
		for (auto x : v)
		{
			cout << x << "  ";
		}
		cout << endl;
	}
}